import urllib.parse
import urllib.request
import json
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
}
for i in range(100):
    url = "https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=%d" %(i)
    request = urllib.request.Request(url,headers=headers)
    response = urllib.request.urlopen(request)
    content = response.read().decode()
    #print(content)  #这个数据是json数据

    data = json.loads(content)
    # print(data)

    data_list = data.get('data')
    for data in data_list:
        title = data['title']
        casts = data['casts']
        rate = data['rate']
        with open('douban.txt','a+',encoding='utf-8') as fp:
            fp.write(str((title,content,rate))+"\n")
            fp.flush() #不回车也会往里写